﻿//======================================================================
//
//	Cross-Platform XNA Menu System
//  http://cpxms.codeplex.com/
//
//  All code and content licensed under Microsoft Public License (Ms-PL)	
//
//======================================================================
//
//	File:		MenuInput.cs
//
//	Author:		MJP
//
//	Date:		08/05/09
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.Drawing.Design;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

// NOTE:  Moved here from MenuProject.cs, 08/08/09

namespace CPXMenu
{
    /// <summary>
    /// Contains input data that is sent to the MenuProject 
    /// </summary>
    public struct MenuInput
    {
        public Point CursorPos;
        public bool Select;
        public bool Back;
        public bool Up;
        public bool Down;
        public bool Left;
        public bool Right;

        /// <summary>
        /// Gets an empty MenuInput (no input)
        /// </summary>
        public static MenuInput Empty
        {
            get
            {
                MenuInput empty = new MenuInput();
                empty.CursorPos = new Point(-100, -100);
                return empty;
            }
        }

#if WINDOWS
        /// <summary>
        /// Gets default input for mouse + keyboard
        /// </summary>
        /// <returns>A MenuInput containing default mouse + keyboard input</returns>
        public static MenuInput GetMouseKBInput()
        {
            MenuInput input = new MenuInput();

            KeyboardState keyState = Keyboard.GetState();
            MouseState mouseState = Mouse.GetState();

            input.CursorPos.X = mouseState.X;
            input.CursorPos.Y = mouseState.Y;

            input.Select = mouseState.LeftButton == ButtonState.Pressed || keyState.IsKeyDown(Keys.Space) || keyState.IsKeyDown(Keys.Enter);
            input.Back = keyState.IsKeyDown(Keys.Escape);

            input.Up = keyState.IsKeyDown(Keys.Up);
            input.Down = keyState.IsKeyDown(Keys.Down);
            input.Left = keyState.IsKeyDown(Keys.Left);
            input.Right = keyState.IsKeyDown(Keys.Right);

            return input;
        }
#endif
        /// <summary>
        /// Gets default input for a gamepad
        /// </summary>
        /// <param name="index">The PlayerIndex at which to query for gamepad input</param>
        /// <returns>A MenuInput containing default gamepad input</returns>
        public static MenuInput GetGamepadInput(PlayerIndex index)
        {
            MenuInput input = MenuInput.Empty;

            GamePadState padState = GamePad.GetState(index);
            if (!padState.IsConnected)
                return input;

            input.Select = padState.Buttons.A == ButtonState.Pressed || padState.Buttons.Start == ButtonState.Pressed;
            input.Back = padState.Buttons.B == ButtonState.Pressed || padState.Buttons.Back == ButtonState.Pressed;

            input.Up = padState.DPad.Up == ButtonState.Pressed;
            input.Down = padState.DPad.Down == ButtonState.Pressed;
            input.Left = padState.DPad.Left == ButtonState.Pressed;
            input.Right = padState.DPad.Right == ButtonState.Pressed;

            return input;
        }

#if WINDOWS
        /// <summary>
        /// Gets default input for both a Gamepad and Mouse + KB
        /// </summary>
        /// <param name="index">The PlayerIndex at which to query for gamepad input</param>
        /// <returns>A MenuInput containing default input for both a Gamepad and Mouse + KB</returns>
        public static MenuInput GetCombinedInput(PlayerIndex index)
        {
            MenuInput mouseKBInput = GetMouseKBInput();
            MenuInput gamepadInput = GetGamepadInput(index);

            mouseKBInput.Select = mouseKBInput.Select || gamepadInput.Select;
            mouseKBInput.Back = mouseKBInput.Back || gamepadInput.Back;

            mouseKBInput.Up = mouseKBInput.Up || gamepadInput.Up;
            mouseKBInput.Down = mouseKBInput.Down || gamepadInput.Down;
            mouseKBInput.Left = mouseKBInput.Left || gamepadInput.Left;
            mouseKBInput.Right = mouseKBInput.Right || gamepadInput.Right;

            return mouseKBInput;
        }
#endif
    }
}